package com.ojoin.trade.common.web.service.impl;

import com.google.common.collect.Lists;
import com.ojoin.trade.common.SqlId;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.dao.BaseDao;
import com.ojoin.trade.common.web.domain.IDentifiable;
import com.ojoin.trade.common.web.service.BaseService;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 基础Service服务接口实现类
 */
public abstract class BaseServiceImpl<T extends IDentifiable> implements
		BaseService<T> {

	/**
	 * 获取基础数据库操作类
	 * 
	 * @return
	 */
	protected abstract BaseDao<T> getBaseDao();

	@Override
	public <V extends T> V queryOne(T query) {
		return getBaseDao().selectOne(query);
	}

	@Override
	public <V extends T> V queryById(int id) {
		return getBaseDao().selectById(id);
	}

	@Override
	public <V extends T> List<V> queryList(T query) {
		return getBaseDao().selectList(query);
	}

	@Override
	public <V extends T> List<V> queryAll() {
		return getBaseDao().selectAll();
	}

	@Override
	public <K, V extends T> Map<K, V> queryMap(T query, String mapKey) {
		return getBaseDao().selectMap(query, mapKey);
	}

	@Override
	public Long queryCount() {
		return getBaseDao().selectCount();
	}

	@Override
	public Long queryCount(T query) {
		return getBaseDao().selectCount(query);
	}

	@Override
	public int insert(T entity) {
		return getBaseDao().insert(entity);
	}

	
	
	@Override
	public int delete(T query) {
		return getBaseDao().delete(query);
	}
	
	@Override
	public int save(T query) {
		if(null==query.getId()){
			return	getBaseDao().insert(query);
		}else{
			return getBaseDao().updateById(query);
		}
	}

	@Override
	public int deleteById(int id) {
		return getBaseDao().deleteById(id);
	}

	@Override
	public int deleteAll() {
		return getBaseDao().deleteAll();
	}

	@Override
	public int updateById(T entity) {
		return getBaseDao().updateById(entity);
	}

	@Override
	public int updateByIdSelective(T entity) {
		return getBaseDao().updateByIdSelective(entity);
	}

	@Override
	public void deleteByIdInBatch(List<Integer> idList) {
		getBaseDao().deleteByIdInBatch(idList);
	}

	@Override
	public int[] insertInBatch(List<T> entityList) {
		if (!CommonUtils.isEmpty(entityList)) {
			return getBaseDao().insertInBatch(entityList);
		}
		return null;
	}
 
	@Override
	public int[] insertBatch(List<T> entityList) {
		if (!CommonUtils.isEmpty(entityList)) {
			return getBaseDao().insertBatch(entityList);
		}
		return null;
	}

	@Override
	public void updateInBatch(List<T> entityList) {
		getBaseDao().updateInBatch(entityList);
	}

	@Override
	public <V extends T> List<V> queryList(T query, Pageable pageable) {
		return getBaseDao().selectList(query, pageable);
	}

	public <V extends T> Page<V> queryPageList(T query, Pageable pageable) {
		return getBaseDao().selectPageList(query, pageable);
	}

	@Override
	public <K, V extends T> Map<K, V> queryMap(T query, String mapKey,
			Pageable pageable) {
		return getBaseDao().selectMap(query, mapKey, pageable);
	}

	/**
	 * JDBC Batch
	 * @param entityList
	 * @return  success
	 */
	public int[] updateJDBCBatch(List<T> entityList) {
		return getBaseDao().executeJDBCBatch(SqlId.SQL_UPDATE_BY_ID,entityList);
	}

    /**
     * JDBC Batch
     * @param entityList
     * @return  success
     */
    public int[] insertJDBCBatch(List<T> entityList) {
        return getBaseDao().executeJDBCBatch(SqlId.SQL_INSERT,entityList);
    }

}
